package com.small.view.custview.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.small.R;
 
public class MyPopMenu {

	private Button btn1, btn2;
	private PopupWindow mPopupWindow;
	private View view;

	private ListView list_view1;
	private ListView list_view2;
	private Context context;
	
	private PopFirstAdapter list_first_adapter;
	private PopSecondAdapter list_second_adapter;
	private PopSingleAdapter list_single_adapter;
	List<String> list_first_model;
	List<String> list_second_model;
//	List<SQParentModel> list = null;

	public  final int VIEW_NONE_TAG = -1;
	
	public MyPopMenu(Context ctx) {
		this.context = ctx; 
//		this.resId = reslayoutId;
//		initPopupWindow();
	}

	public MyPopMenu(Context ctx, ArrayList<String> first_model,ArrayList<String> second_model) {
		this.context = ctx; 
		
//		this.resId = ressID;
//		this.drawableId = drawBgId;
		this.list_first_model = first_model;
		this.list_second_model = second_model;
				
//		initPopupWindow();
	}
 

	public void initPopupWindow(int drawBgId, int resId,int ptag,int ctag) {
		LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = mLayoutInflater.inflate(resId, null);
		Resources res = context.getResources();
		float popheitht = res.getDimension(R.dimen.popheight);
		mPopupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, (int)popheitht);
		// mPopupWindow.setBackgroundDrawable(new
		// BitmapDrawable());//必须设置background才能消失
		mPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(drawBgId));
		mPopupWindow.setOutsideTouchable(true);

		// 自定义动画
		// mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
		// 使用系统动画
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
		mPopupWindow.setFocusable(true);
		mPopupWindow.update();
		mPopupWindow.setTouchable(true);
		list_view1 = (ListView) view.findViewById(R.id.listView1);
		list_view1.setFocusable(true);
		list_view1.setFocusableInTouchMode(true);
		list_view2 = (ListView) view.findViewById(R.id.list2); 
		list_view2.setFocusable(true); 
		list_view2.setFocusableInTouchMode(true); 
		
		if (ptag == VIEW_NONE_TAG) {
			list_view2.setVisibility(View.GONE);
			list_single_adapter = new PopSingleAdapter(context, list_second_model,ctag);
			list_view1.setAdapter(list_single_adapter); 
			list_view1.setOnItemClickListener( list_single_adapter);
		}else{ 
			list_view2.setVisibility(View.VISIBLE); 
			list_first_adapter = new PopFirstAdapter(context, list_first_model,ptag);
			list_view1.setAdapter(list_first_adapter);
			list_view1.setOnItemClickListener(list_first_adapter);
			list_second_adapter = new PopSecondAdapter(context, list_second_model,ctag);
			list_view2.setAdapter(list_second_adapter); 
			list_view2.setOnItemClickListener( list_second_adapter);
		} 
	}
	
	public void show(View view) {
		if(!mPopupWindow.isShowing()){
			mPopupWindow.showAsDropDown(view,0,0);
//			mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		}
	} 
	public void dismiss() {
		if(mPopupWindow.isShowing()){
			mPopupWindow.dismiss();
		}
	} 
	public void refresh(int firstId,int secondId, ArrayList<String> list) { 
		list_first_adapter.setSelectItem(firstId);
		list_second_adapter.setSelectItem(secondId,list);
	} 
} 